package com.augrain.microframework.limit.breaker;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author biaoy
 * @since 2020/12/23
 */
public class CircuitBreakerManager {
    //保存CircuitBreaker的是例，避免每次都new对象
    private static final Map<Class<? extends CircuitBreaker>, CircuitBreaker> CircuitBreakerMap = new ConcurrentHashMap<>();

    public static CircuitBreaker getHandler(Class<? extends CircuitBreaker> breaker) throws Exception {
        CircuitBreaker circuitBreaker = CircuitBreakerMap.get(breaker);
        if (circuitBreaker == null) {
            synchronized (breaker) {
                if (circuitBreaker == null) {
                    circuitBreaker = breaker.newInstance();
                    CircuitBreakerMap.put(breaker, circuitBreaker);
                }
            }
        }
        return circuitBreaker;
    }

}
